package co.edu.poli.ficb.agl.automatable.gui;

import java.awt.Shape;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.Collection;

import co.edu.poli.ficb.agl.automatable.graph.Arista;
import co.edu.poli.ficb.agl.automatable.graph.Vertice;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.util.Context;
import edu.uci.ics.jung.graph.util.EdgeIndexFunction;
import edu.uci.ics.jung.graph.util.Pair;
import edu.uci.ics.jung.visualization.decorators.EdgeShape;
import edu.uci.ics.jung.visualization.decorators.EdgeShape.Loop;

public class PA2QuadCurve extends EdgeShape.QuadCurve<Vertice, Arista>{
	
	/**
     * a convenience instance for other edge shapes to use
     * for self-loop edges where parallel instances will not
     * overlay each other.
     */
    @SuppressWarnings("rawtypes")
	protected static Loop loop = new Loop();
        
    /**
     * Singleton instance of the Line2D edge shape
     */
    private static Line2D line = new Line2D.Float(0.0f, 0.0f, 1.0f, 0.0f);

    
	@Override
	public Shape transform(Context<Graph<Vertice, Arista>, Arista> context) {
		
		Graph<Vertice, Arista> grafo = context.graph;
		
    	Arista arista = context.element;
    	
        Pair<Vertice> endpoints = grafo.getEndpoints(arista);
        
        Collection<Arista> aristas = new ArrayList<Arista>();
        
        aristas.addAll(grafo.findEdgeSet(grafo.getSource(arista), grafo.getDest(arista)));
        
        aristas.addAll(grafo.findEdgeSet(grafo.getDest(arista), grafo.getSource(arista)));
        
        if (endpoints != null) {
        	boolean isLoop = endpoints.getFirst().equals(endpoints.getSecond());
        	if (isLoop) {
        		return loop.transform(context);
        	}
        }
        
        if (aristas.size() == 1)
        	return line;
        else
        	return super.transform(context);
	}
}
